"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 二级分区
Case Name   : list_hash二级分区表列约束:分区名称为数字/字符串开头
Create At   : 2022/2/24
Owner       : opentestcase012
Description :
    1.创建表空间
    2.创建二级分区表,分区名称为数字开头
    3.创建二级分区表并插入数据,分区名称为字符串，字符串数字开头
    4.查询特殊分区
    5.查询数字开头的字符串的二级分区数据
    6.查询其他指定分区数据
    7.二级分区键创建并查询本地索引
    8.清理环境
Expect      :
    1.成功
    2.合理报错
    3.成功
    4.成功
    5.成功
    6.成功
    7.成功，local索引
    8.清理成功
History     :
"""
import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class Subpartition(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.primaryNode = Node('PrimaryDbUser')
        self.constant = Constant()
        self.t_name = 't_subpartition_0318'
        self.ts_name = 'ts_subpartition_0318'

    def test_subpartition(self):
        text = '-----step1:创建表空间 expect:成功-----'
        self.log.info(text)
        create_sql = f"drop table if exists {self.t_name};" \
            f"drop tablespace if exists {self.ts_name};" \
            f"create tablespace {self.ts_name} relative location " \
            f"'{self.ts_name}';"
        msg = self.pri_sh.execut_db_sql(create_sql)
        self.log.info(msg)
        self.assertIn(self.constant.TABLESPCE_CREATE_SUCCESS, msg,
                      '执行失败:' + text)

        text = '-----step2:创建二级分区表,分区名称为数字开头 expect:合理报错-----'
        self.log.info(text)
        create_sql = f'create table if not exists {self.t_name}(col_1 int ,' \
            f'col_2 int ,col_3 int ,col_4 int ) tablespace {self.ts_name} ' \
            f'partition by range (col_1) subpartition by hash (col_2)' \
            f'( partition p_range_1 values less than( -10 )' \
            f'  ( subpartition p_hash_1_1 , subpartition p_hash_1_2 ,' \
            f'    subpartition 11_o),' \
            f'  partition p_range_2 values less than( 20 ),' \
            f'  partition p_range_3 values less than( 30)' \
            f'  ( subpartition p_hash_3_1 , subpartition p_hash_3_2 ,' \
            f'    subpartition p_hash_3_3),' \
            f'    partition p_range_4 values less than( 50)' \
            f'  ( subpartition p_hash_4_1 , subpartition p_hash_4_2 ,' \
            f'    subpartition t_subpartition_0350),' \
            f'  partition p_range_5 values less than( maxvalue )' \
            f') enable row movement;'
        msg = self.pri_sh.execut_db_sql(create_sql)
        self.log.info(msg)
        self.assertIn('syntax error at or near "11"', msg, '执行失败:' + text)

        text = '-----step3:创建二级分区表并插入数据,分区名称为字符串，字符串中由数字开头 expect:成功-----'
        self.log.info(text)
        create_sql = f'drop table if exists {self.t_name};' \
            f'create table if not exists {self.t_name}(col_1 int ,' \
            f'col_2 int ,col_3 int ,col_4 int ) tablespace {self.ts_name} ' \
            f'partition by range (col_1) subpartition by hash (col_2)' \
            f'( partition p_range_1 values less than( -10 )' \
            f'  ( subpartition p_hash_1_1 , subpartition p_hash_1_2 ,' \
            f'    subpartition p_hash_1_3),' \
            f'  partition p_range_2 values less than( 20 ),' \
            f'  partition p_range_3 values less than( 30)' \
            f'  ( subpartition p_hash_3_1 , subpartition p_hash_3_2 ,' \
            f'    subpartition p_hash_3_3),' \
            f'    partition p_range_4 values less than( 50)' \
            f'  ( subpartition \\"11_o\\"),' \
            f'  partition p_range_5 values less than( maxvalue )' \
            f') enable row movement;' \
            f'insert into {self.t_name} values(1,1,1,1),(5,5,5,5),' \
            f'(49,9,9,9),(11,11,1,1),(15,15,5,5),(18,81,8,8),(29,91,9,9),' \
            f'(-21,11,1,1),(-15,15,5,5),(-18,81,8,8),(-29,91,9,9),' \
            f'(-18,1,1,1),(-15,-815,5,5),(-18,7,8,8),(-29,829,9,9),' \
            f'(8,8,8,8),(-18,18795,1,1); '
        msg = self.pri_sh.execut_db_sql(create_sql)
        self.log.info(msg)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, msg, '执行失败:' + text)
        self.assertIn(self.constant.INSERT_SUCCESS_MSG, msg, '执行失败:' + text)

        text = '-----step4:查询特殊分区 expect:成功-----'
        self.log.info(text)
        sql = f"select relname from pg_partition where parentid = (" \
            f"select oid from pg_class where relname = '{self.t_name}')" \
            f" order by relname;" \
            f"select a.relname from pg_partition a,(select oid from " \
            f"pg_partition where parentid = (select oid from pg_class " \
            f"where relname = '{self.t_name}')) b " \
            f"where a.parentid = b.oid order by a.relname;"
        msg = self.pri_sh.execut_db_sql(sql)
        self.log.info(msg)
        self.assertIn(self.t_name, msg, '执行失败:' + text)
        self.assertIn('p_range_4', msg, '执行失败:' + text)
        self.assertIn('11_o', msg, '执行失败:' + text)

        text = '-----step5:查询数字开头的字符串的二级分区数据 expect:成功-----'
        self.log.info(text)
        sql = f'select * from {self.t_name} subpartition(\\"11_o\\");'
        msg = self.pri_sh.execut_db_sql(sql)
        self.log.info(msg)
        self.assertIn('1 row', msg, '执行失败:' + text)

        text = '-----step6:查询其他指定分区数据 expect:成功-----'
        self.log.info(text)
        sql = f"select * from {self.t_name} " \
            f"subpartition(p_range_2_subpartdefault1);" \
            f"select * from {self.t_name} " \
            f"subpartition(p_range_5_subpartdefault1);" \
            f"select max(col_4) from {self.t_name} partition(p_range_2);" \
            f"select count(*) from {self.t_name} " \
            f"subpartition(p_range_2_subpartdefault1);" \
            f"select col_2 from {self.t_name} " \
            f"subpartition(p_range_2_subpartdefault1);"
        msg = self.pri_sh.execut_db_sql(sql)
        self.log.info(msg)
        self.assertIn('6 rows', msg.splitlines()[-1], '执行失败:' + text)
        self.assertIn('1 row', msg.splitlines()[-11], '执行失败:' + text)
        self.assertIn('1 row', msg.splitlines()[-16], '执行失败:' + text)
        self.assertIn('0 row', msg.splitlines()[-21], '执行失败:' + text)
        self.assertIn('6 rows', msg.splitlines()[-25], '执行失败:' + text)

        text = '-----step7:二级分区键创建并查询本地索引 expect:成功，local索引-----'
        self.log.info(text)
        sql = f"create index on {self.t_name}(col_2) local;" \
            f"\d+ {self.t_name}"
        msg = self.pri_sh.execut_db_sql(sql)
        self.log.info(msg)
        self.assertIn(self.constant.CREATE_INDEX_SUCCESS, msg, '执行失败:' + text)
        self.assertIn('btree (col_2) LOCAL', msg, '执行失败:' + text)

    def tearDown(self):
        self.log.info('-----step8:清理环境-----')
        text_1 = '-----删除表 expect:成功-----'
        self.log.info(text_1)
        del_cmd = f'drop table if exists {self.t_name};' \
            f'drop tablespace if exists {self.ts_name};'
        msg = self.pri_sh.execut_db_sql(del_cmd)
        self.log.info(msg)
        self.assertIn(self.constant.TABLESPCE_DROP_SUCCESS, msg,
                      '执行失败:' + text_1)
        self.assertIn(self.constant.TABLE_DROP_SUCCESS, msg, '执行失败:' + text_1)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
